package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.VideoPlayerVRActivity;
import com.v1.video.db.dao.VideoTB;
import com.v1.video.util.Utility;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类直播列表适配器
 * 
 * @author wangfang
 * 
 */
public class ClassifyLiveListAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private int mHeight;
	private List<VideoTB> mLstData;
	private GridView mGv;
	private float mPadding;

	public ClassifyLiveListAdapter(Context context, GridView gv) {
		this.mContext = context;
		this.mGv = gv;
		mLayoutInflater = LayoutInflater.from(context);
		mPadding = mContext.getResources().getDimension(
				R.dimen.classifylivelist_list_padding);
		float itempadding = mContext.getResources().getDimension(
				R.dimen.classifylivelist_item_padding);
		mHeight = Utils.computeImageHeight(mContext,
				(int) (mPadding * 2 + itempadding), 2);
		mLstData = new ArrayList<VideoTB>();
	}

	public List<VideoTB> getLstData() {
		return mLstData;
	}

	public void setLstData(List<VideoTB> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}
	}

	public void setAddLstData(List<VideoTB> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData.addAll(mLstData);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.activity_classifylivelist_item, null);
			holder = new Holder();
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);
			holder.update();
		} else {
			holder = (Holder) convertView.getTag();
		}
		VideoTB info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getTitle())) {
			holder.title.setText(info.getTitle());
		} else {
			holder.title.setText("");
		}
		if (!TextUtils.isEmpty(info.getDuration())) {
			holder.time.setText(Utility.getDuration(info.getDuration()));
		} else {
			holder.time.setText("00:00");
		}
		holder.time.setTag(position);
		holder.title.setTag(convertView);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, mHeight);
		if (position % 2 == 0) {
			params.setMargins((int)mPadding, (int)mPadding, 0, 0);
		} else {
			params.setMargins(0, (int)mPadding, (int)mPadding, 0);
		}
		holder.image.setLayoutParams(params);
		ImageLoader.getInstance().displayImage(info.getPic(), holder.image,
				Constant.VIDEO_10_16_OPTION);
		holder.image.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(mContext, VideoPlayerVRActivity.class);
				ArrayList<String> listUrl = new ArrayList<String>();
				for (int i = 0; i < mLstData.size(); i++) {
					listUrl.add(mLstData.get(i).getVideoUrl());
				}
				intent.putStringArrayListExtra("videourl", listUrl);
				intent.putExtra("pos", position);
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	private class Holder {
		public ImageView image;
		public TextView time;
		public TextView title;

		public void update() {
			// 精确计算GridView的item高度
			title.getViewTreeObserver().addOnGlobalLayoutListener(
					new OnGlobalLayoutListener() {
						public void onGlobalLayout() {
							int position = (Integer) time.getTag();
							if (position > 0 && position % 2 == 1) {
								if (title.getTag() != null) {
									View v = (View) title.getTag();
									int height = v.getHeight();
									View view = mGv.getChildAt(position - 1);
									if (view != null) {
										int lastheight = view.getHeight();
										if (height > lastheight) {
											view.setLayoutParams(new GridView.LayoutParams(
													GridView.LayoutParams.MATCH_PARENT,
													height));
										} else if (height < lastheight) {
											v.setLayoutParams(new GridView.LayoutParams(
													GridView.LayoutParams.MATCH_PARENT,
													lastheight));
										}
									}
								}
							}
						}
					});
		}
	}

}
